﻿// This source file is part of WallpaperControl, a tool for automatic changing 
// of the desktop wallpaper.
// 
// Visit http://www.wallpaper-control.xe.cx/ and 
// http://wpcontrol.codeplex.com/ for more.
//
// Copyright (c) 2009 Niklas Kroll
//
// Licensed under the GPLv2.
// See http://www.gnu.org/licenses/gpl-2.0.html for details.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.Xml.XPath;
using System.Windows.Forms;
using System.IO;
using System.Security;

namespace wpControl1
{
    /// <summary>
    /// Class that manages loading and saving of the configuration files.
    /// </summary>
    class CfgFileManager
    {
        /// <summary>
        /// Saves the list of images to an XML document.
        /// </summary>
        internal void SaveImageListFile()
        {
            try
            {
                XmlDocument imgDoc = new XmlDocument();
                // Create root element
                XmlNode listNode;
                listNode = imgDoc.CreateElement("imageList");
                imgDoc.AppendChild(listNode);

                XmlNode imageNode;
                XmlNode pathNode;
                XmlNode styleNode;
                // For every single image create tag and save path and style
                for (int i = 0; i < Program.ImageManager.Images.Count; i++)
                {
                    imageNode = imgDoc.CreateElement("image");
                    listNode.AppendChild(imageNode);
                    pathNode = imgDoc.CreateElement("path");
                    pathNode.InnerText = Program.ImageManager.Images[i];
                    imageNode.AppendChild(pathNode);
                    styleNode = imgDoc.CreateElement("style");
                    styleNode.InnerText = Program.ZoomStyleManager.Styles[i].ToString();
                    imageNode.AppendChild(styleNode);
                }
                // Save XML document
                imgDoc.Save("images.xml");
            }
            catch (XmlException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveImgList_Xml"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveImgList_InvalidOp"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (IOException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveImgList_IO"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (SecurityException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveImgList_Security"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Loads the list of images from the XML document.
        /// </summary>
        internal void LoadImageListFile()
        {
            try
            {
                System.Xml.XmlDocument XMLdoc = new System.Xml.XmlDocument();
                XMLdoc.Load("images.xml");
                // Get root element
                XmlElement root = XMLdoc.DocumentElement;
                // Select all path elements
                XmlNodeList pathNodes = root.SelectNodes("/imageList/image/path");
                foreach (XmlNode singlePathNode in pathNodes)
                {
                    // Add path elements to list
                    Program.ImageManager.Images.Add(singlePathNode.InnerText);
                }

                // Select all style elements
                XmlNodeList styleNodes = root.SelectNodes("/imageList/image/style");
                // Add all style elements to list
                foreach (XmlNode singleStyleNode in styleNodes)
                {
                    if (singleStyleNode.InnerText == Wallpaper.Style.Stretched.ToString())
                    {
                        Program.ZoomStyleManager.Styles.Add(Wallpaper.Style.Stretched);
                    }
                    else if (singleStyleNode.InnerText == Wallpaper.Style.Tiled.ToString())
                    {
                        Program.ZoomStyleManager.Styles.Add(Wallpaper.Style.Tiled);
                    }
                    else if (singleStyleNode.InnerText == Wallpaper.Style.Centered.ToString())
                    {
                        Program.ZoomStyleManager.Styles.Add(Wallpaper.Style.Centered);
                    }
                }
            }
            catch (XmlException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadImgList_Xml"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadImgList_NotFound"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (SecurityException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadImgList_Security"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Saves the condition into the XML document.
        /// </summary>
        internal void SaveConditionFile()
        {
            try
            {
                XmlDocument condDoc = new XmlDocument();

                // Create root element
                XmlNode condNode;
                condNode = condDoc.CreateElement("condition");
                condDoc.AppendChild(condNode);

                // Save type of condition (numeric)
                XmlNode typeNode;
                typeNode = condDoc.CreateElement("typeID");
                typeNode.InnerText = Program.ConditionManager.CondType.ToString();
                condNode.AppendChild(typeNode);

                // Save condition value
                XmlNode valueNode;
                valueNode = condDoc.CreateElement("value");
                if (Program.ConditionManager.CondType == 1)
                {
                    valueNode.InnerText = Program.ConditionManager.ChangeTime.ToString();
                }
                else if (Program.ConditionManager.CondType == 2)
                {
                    valueNode.InnerText = Program.ConditionManager.ChangeInterval.ToString();
                }
                else if (Program.ConditionManager.CondType == 3)
                {
                    valueNode.InnerText = Program.ConditionManager.ChangeIndex.ToString();
                }
                condNode.AppendChild(valueNode);

                // Save file
                condDoc.Save("condition.xml");
            }
            catch (XmlException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveCond_Xml"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveCond_InvalidOp"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (IOException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveCond_IO"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (SecurityException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveCond_Security"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Loads the condition from the XML document.
        /// </summary>
        internal void LoadConditionFile()
        {
            try
            {
                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.Load("condition.xml");

                // Get condition type 
                XmlNode typeNode = xmlDoc.SelectSingleNode("/condition/typeID");
                Program.ConditionManager.CondType = Convert.ToInt32(typeNode.InnerText);

                // Get condition value
                XmlNode valueNode = xmlDoc.SelectSingleNode("/condition/value");
                if (Program.ConditionManager.CondType == 1)
                {
                    Program.ConditionManager.ChangeTime = Convert.ToDateTime(valueNode.InnerText);
                }
                else if (Program.ConditionManager.CondType == 2)
                {
                    Program.ConditionManager.ChangeInterval = Convert.ToInt32(valueNode.InnerText);
                }
                else if (Program.ConditionManager.CondType == 3)
                {
                    Program.ConditionManager.ChangeIndex = Convert.ToInt32(valueNode.InnerText);
                }
            }
            catch (XmlException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadCond_Xml"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadCond_NotFound"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (SecurityException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadCond_Security"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (IOException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadCond_IO"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (FormatException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadCond_Format"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Saves the settings from the ControlPanel into a XML document.
        /// </summary>
        internal void SaveSettingsFile()
        {
            try
            {
                XmlDocument settingsDoc = new XmlDocument();
                XmlNode settingsListNode;

                // Create root element
                settingsListNode = settingsDoc.CreateElement("settings");
                settingsDoc.AppendChild(settingsListNode);

                // Create element with value for each single settings
                XmlNode settingNode;
                settingNode = settingsDoc.CreateElement("startWithSys");
                settingNode.InnerText = Program.SettingsManager.StartWithWindows.ToString();
                settingsListNode.AppendChild(settingNode);

                settingNode = settingsDoc.CreateElement("startControlPanel");
                settingNode.InnerText = Program.SettingsManager.StartWithControlPanel.ToString();
                settingsListNode.AppendChild(settingNode);

                settingNode = settingsDoc.CreateElement("changeInfo");
                settingNode.InnerText = Program.SettingsManager.StartWithControlPanel.ToString();
                settingsListNode.AppendChild(settingNode);

                settingNode = settingsDoc.CreateElement("runningInfo");
                settingNode.InnerText = Program.SettingsManager.RunningInfo.ToString();
                settingsListNode.AppendChild(settingNode);

                settingNode = settingsDoc.CreateElement("repeatingInfo");
                settingNode.InnerText = Program.SettingsManager.LoopingInfo.ToString();
                settingsListNode.AppendChild(settingNode);

                settingNode = settingsDoc.CreateElement("repeatWallpapers");
                settingNode.InnerText = Program.SettingsManager.LoopWallpapers.ToString();
                settingsListNode.AppendChild(settingNode);

                // Save document
                settingsDoc.Save("settings.xml");
            }
            catch (XmlException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveSettings_Xml"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (InvalidOperationException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveSettings_InvalidOp"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (IOException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveSettings_IO"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (SecurityException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_SaveSettings_Security"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Loads the settings from the XML document.
        /// </summary>
        internal void LoadSettingsFile()
        {
            try
            {
                XmlDocument settingsDoc = new XmlDocument();
                settingsDoc.Load("settings.xml");

                // Select every single setting and convert it back to bool.
                XmlNode currentNode = settingsDoc.SelectSingleNode("/settings/startWithSys");
                Program.SettingsManager.StartWithWindows = Convert.ToBoolean(currentNode.InnerText);

                currentNode = settingsDoc.SelectSingleNode("/settings/startControlPanel");
                Program.SettingsManager.StartWithControlPanel = Convert.ToBoolean(currentNode.InnerText);

                currentNode = settingsDoc.SelectSingleNode("/settings/changeInfo");
                Program.SettingsManager.ChangeInfo = Convert.ToBoolean(currentNode.InnerText);

                currentNode = settingsDoc.SelectSingleNode("/settings/runningInfo");
                Program.SettingsManager.RunningInfo = Convert.ToBoolean(currentNode.InnerText);

                currentNode = settingsDoc.SelectSingleNode("/settings/repeatingInfo");
                Program.SettingsManager.LoopingInfo = Convert.ToBoolean(currentNode.InnerText);

                currentNode = settingsDoc.SelectSingleNode("/settings/repeatWallpapers");
                Program.SettingsManager.LoopWallpapers = Convert.ToBoolean(currentNode.InnerText);
            }
            catch (XmlException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadSettings_Xml"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                Program.SettingsManager.LoadPresets();
            }
            catch (FileNotFoundException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadSettings_NotFound"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                Program.SettingsManager.LoadPresets();
            }
            catch (SecurityException)
            {
                MessageBox.Show(Program.LanguageManager.GetString("Error_LoadSettings_Security"), Program.LanguageManager.GetString("Caption_Error"), MessageBoxButtons.OK, MessageBoxIcon.Error);
                Program.SettingsManager.LoadPresets();
            }
        }
    }
}
